package mamikeji.liaowa.ui.activity.circle;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mamikeji.liaowa.R;
import mamikeji.liaowa.application.MyApplication;
import mamikeji.liaowa.bean.FriendCircleInfoBean;
import mamikeji.liaowa.bean.FriendPraiseBean;
import mamikeji.liaowa.bean.Image;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.ui.adapter.CircleCommentAdapter;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;
import mamikeji.liaowa.utils.MaxTextLength;
import mamikeji.liaowa.utils.MultiStateView;
import mamikeji.liaowa.utils.SizeHelper;
import mamikeji.liaowa.utils.SystemUtils;
import mamikeji.liaowa.widget.ForPopWin;
import mamikeji.liaowa.widget.ninegrid.CustomImageView;
import mamikeji.liaowa.widget.ninegrid.NineGridlayout;
import mamikeji.liaowa.widget.ninegrid.ScreenTools;

public class CircleDetailsActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "CircleDetailsActivity";
    private ListView listView_circle_details_data;
    private String fcid;
    private List<FriendCircleInfoBean.FriendchatEntity> friendchatEntities = new ArrayList<>();

    private EditText editText_dynamic_details_comment;
    private TextView textView_dynamic_details_release;
    private MultiStateView multiStateView_dynamic_details_comment;

    private View header_view_1, header_view_2;
    private TextView textView_article_details_content, textView_dynamic_creattime_details, textView_dynamic_zannum_details, textView_dynamic_recommend_details, textView_dynamic_circle_userzan_details, textView_dynamic_circle_userzan_num;
    private NineGridlayout iv_ngrid_layout_details;
    private CustomImageView iv_oneimage_details;
    private LinearLayout linearLayout_dynamic_zannum, linearLayout_dynamic_circle_userzan;
    private ImageView imageView_dynamic_zan_details;

    private CircleCommentAdapter circleCommentAdapter;

    private FriendCircleInfoBean friendCircleInfoBean;

    Boolean aBoolean , bool_circle_refresh;

    private String commentID;
    private FriendCircleInfoBean.FriendchatEntity entity;

    private ForPopWin forPopWin;

    private Dialog dialog_share;

    private int commentNum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_details);
        getBase();
        setTitleLeftImage(R.drawable.icon_fanhuibai_n_fff, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bool_circle_refresh){
                    setResult(RESULT_OK, new Intent());
                }
                finish();
            }
        });

        Intent intent = getIntent();
        fcid = intent.getStringExtra("fcid");

        setTitleRightImage(R.drawable.icon_gengduo_n, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //以某个控件的x和y的偏移量位置开始显示窗口
                forPopWin.showAsDropDown((ImageView) findViewById(R.id.iv_header_right), 0, 0);
                //如果窗口存在，则更新
                forPopWin.update();
            }
        });

        commentID = fcid;

        init();
        initData();
        getDialogShare();
    }

    private void init() {
        initPop();
        bool_circle_refresh = false;
        multiStateView_dynamic_details_comment = (MultiStateView) findViewById(R.id.multiStateView_dynamic_details_comment);
        editText_dynamic_details_comment = (EditText) findViewById(R.id.editText_dynamic_details_comment);
        textView_dynamic_details_release = (TextView) findViewById(R.id.textView_dynamic_details_release);

        InputFilter[] filters = new InputFilter[]{new MaxTextLength(400)};
        editText_dynamic_details_comment.setFilters(filters);

        header_view_1 = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.circle_details_layout_header_1, null);
        header_view_2 = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.circle_details_layout_header_2, null);

        linearLayout_dynamic_circle_userzan = (LinearLayout) header_view_1.findViewById(R.id.linearLayout_dynamic_circle_userzan);
        linearLayout_dynamic_zannum = (LinearLayout) header_view_1.findViewById(R.id.linearLayout_dynamic_zannum);
        imageView_dynamic_zan_details = (ImageView) header_view_1.findViewById(R.id.imageView_dynamic_zan_details);

        textView_article_details_content = (TextView) header_view_1.findViewById(R.id.textView_article_details_content);
        textView_dynamic_creattime_details = (TextView) header_view_1.findViewById(R.id.textView_dynamic_creattime_details);
        textView_dynamic_zannum_details = (TextView) header_view_1.findViewById(R.id.textView_dynamic_zannum_details);
        textView_dynamic_recommend_details = (TextView) header_view_1.findViewById(R.id.textView_dynamic_recommend_details);
        textView_dynamic_circle_userzan_details = (TextView) header_view_1.findViewById(R.id.textView_dynamic_circle_userzan_details);
        textView_dynamic_circle_userzan_num = (TextView) header_view_1.findViewById(R.id.textView_dynamic_circle_userzan_num);

        iv_ngrid_layout_details = (NineGridlayout) header_view_1.findViewById(R.id.iv_ngrid_layout_details);
        iv_oneimage_details = (CustomImageView) header_view_1.findViewById(R.id.iv_oneimage_details);

        listView_circle_details_data = (ListView) findViewById(R.id.listView_circle_details_data);
        listView_circle_details_data.addHeaderView(header_view_1);

        circleCommentAdapter = new CircleCommentAdapter(friendchatEntities, this);
        listView_circle_details_data.setAdapter(circleCommentAdapter);
    }

    private void initData() {
        getFriendCircleInfo();

        textView_dynamic_details_release.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (getJudgeLogin()) {
                    if (TextUtils.isEmpty(editText_dynamic_details_comment.getText().toString())) {
                        ForToast.showToast(R.string.aiticle_details_recomment_error);
                        return;
                    }
                    if (commentID == fcid) {
                        getCircleRelease(editText_dynamic_details_comment.getText().toString());
                    } else {
                        getCircleReplys(editText_dynamic_details_comment.getText().toString());
                    }
                }
            }
        });

        linearLayout_dynamic_zannum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (getJudgeLogin()) {
                    getFabulousCircle();
                }
            }
        });

        multiStateView_dynamic_details_comment.getView(MultiStateView.VIEW_STATE_ERROR).findViewById(R.id.button_error_refish)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        multiStateView_dynamic_details_comment.setViewState(MultiStateView.VIEW_STATE_LOADING);
                        getFriendCircleInfo();
                    }
                });

        listView_circle_details_data.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ForLog.i("iiiiiii",""+i);
                if (i == 0 || i == 1) {
                    commentID = fcid;
                    editText_dynamic_details_comment.setHint("说点什么~~~");
                } else {
                    ForLog.i("getCusercode",""+friendCircleInfoBean.getFriendchat().get(i - 2).getRusercode()+"[]");
                    if (friendCircleInfoBean.getFriendchat().get(i - 2).getRusercode().equals("")
                            || friendCircleInfoBean.getFriendchat().get(i - 2).getRusercode() == null){
                        if (!ForPreference.getString(ForContents.USER_CODE).equals(friendCircleInfoBean.getFriendchat().get(i - 2).getCusercode())){
                            commentID = friendCircleInfoBean.getFriendchat().get(i - 2).getRowid() + "";
                            entity = friendCircleInfoBean.getFriendchat().get(i - 2);
                            editText_dynamic_details_comment.setHint("回复" +
                                    friendCircleInfoBean.getFriendchat().get(i - 2).getCname() + "~~~");
                        }else {
                            ForToast.showToast("不能回复自己");
                        }
                    }else if (!ForPreference.getString(ForContents.USER_CODE).equals(friendCircleInfoBean.getFriendchat().get(i - 2).getRusercode())){
                        commentID = friendCircleInfoBean.getFriendchat().get(i - 2).getRowid() + "";
                        entity = friendCircleInfoBean.getFriendchat().get(i - 2);
                        editText_dynamic_details_comment.setHint("回复" +
                                friendCircleInfoBean.getFriendchat().get(i - 2).getCname() + "~~~");
                    }else {
                        ForToast.showToast("不能回复自己");
                    }
                }
            }
        });

    }

    private void initPop() {
        if (forPopWin == null) {
            //自定义的单击事件
            forPopWin = new ForPopWin(CircleDetailsActivity.this, SizeHelper.dp2px(CircleDetailsActivity.this, 110), SizeHelper.dp2px(CircleDetailsActivity.this, 110));
            //监听窗口的焦点事件，点击窗口外面则取消显示
            forPopWin.getContentView().setOnFocusChangeListener(new View.OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus) {
                        forPopWin.dismiss();
                    }
                }
            });
        }
        //设置默认获取焦点
        forPopWin.setFocusable(true);

        forPopWin.setOnItemClickListener(new ForPopWin.OnItemClickListener() {
            @Override
            public void onpop_circle_set_shape() {
                forPopWin.dismiss();
                dialog_share.show();
            }

            @Override
            public void onpop_circle_set_report() {
                if (getJudgeLogin()) {
                    forPopWin.dismiss();
                    Intent intent = new Intent(CircleDetailsActivity.this, CircleReportActivity.class);
                    intent.putExtra("fcid", fcid);
                    intent.putExtra("fcname", friendCircleInfoBean.getFriendcircl().getName());
                    startActivity(intent);
                }
            }
        });
    }

    /**
     * 分享弹框
     */
    public void getDialogShare() {
        dialog_share = new Dialog(this, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(this).inflate(R.layout.diglog_set_shape, null);
        dialog_share.setContentView(view);
        Window dialogWindow = dialog_share.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 0;
        lp.width = (int) getResources().getDisplayMetrics().widthPixels; // 宽度
        dialogWindow.setAttributes(lp);

        LinearLayout linearLayout_share_wechatFriend = (LinearLayout) view.findViewById(R.id.linearLayout_share_wechatFriend);
        LinearLayout linearLayout_share_wechatCircle = (LinearLayout) view.findViewById(R.id.linearLayout_share_wechatCircle);
        LinearLayout linearLayout_share_tencentCircle = (LinearLayout) view.findViewById(R.id.linearLayout_share_tencentCircle);

        linearLayout_share_wechatFriend.setOnClickListener(CircleDetailsActivity.this);
        linearLayout_share_wechatCircle.setOnClickListener(CircleDetailsActivity.this);
        linearLayout_share_tencentCircle.setOnClickListener(CircleDetailsActivity.this);
    }

    /**
     * 分享回调
     *
     * @param anEnum
     */
    private void ShareWeb(Enum anEnum) {
        String[] urls = friendCircleInfoBean.getFriendcircl().getImgurl().split(",");
        UMImage thumb;
        if (urls.length > 0){
            thumb = new UMImage(this, urls[0]+"?x-oss-process=image/resize,w_220");
        }else {
            thumb = new UMImage(this, R.mipmap.ic_launcher);
        }
        UMWeb web = new UMWeb(HttpApi.ROOT_PATH + HttpApi.CIRCLE_H5 + fcid);
        web.setThumb(thumb);
        web.setDescription(friendCircleInfoBean.getFriendcircl().getContent());
        web.setTitle(friendCircleInfoBean.getFriendcircl().getName()+"的动态");
        new ShareAction(this).withMedia(web).setPlatform((SHARE_MEDIA) anEnum).setCallback(new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                ForToast.showToast("后台正在玩儿命加载~~~");
            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
                ForToast.showToast("分享成功！");
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                ForLog.i("throwable", "" + throwable.toString());
                ForToast.showToast("分享失败！");
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                ForToast.showToast("取消分享！");
            }
        }).share();
    }

    /**
     * 点赞
     */
    private void getFabulousCircle() {
        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.FC_PRAISE_USERCODE, ForPreference.getString(ForContents.USER_CODE));
        formMap.put(HttpKey.FC_PRAISE_FCID, friendCircleInfoBean.getFriendcircl().getFcid() + "");
        formMap.put(HttpKey.FC_PRAISE_TYPE, "2");
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.FRIEND_CIRCLE_PRAISE, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
            }

            @Override
            public void onResponse(String response) {
                ForLog.e(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        FriendPraiseBean friendPraiseBean = new Gson().fromJson(response, FriendPraiseBean.class);
                        aBoolean = !aBoolean;
                        if (friendPraiseBean != null) {
                            bool_circle_refresh = true;
                            imageView_dynamic_zan_details.setImageResource(aBoolean ? R.drawable.icon_dianzan_h : R.drawable.icon_dianzan_n);
                            textView_dynamic_zannum_details.setText(friendPraiseBean.getTotal() + "");
                            if (friendPraiseBean.getTotal() > 0) {
                                linearLayout_dynamic_circle_userzan.setVisibility(View.VISIBLE);
                                textView_dynamic_circle_userzan_details.setText(friendPraiseBean.getName() + "");
                                textView_dynamic_circle_userzan_num.setText("等" + friendPraiseBean.getTotal() + "人觉得很赞");
                            } else {
                                linearLayout_dynamic_circle_userzan.setVisibility(View.GONE);
                            }
                        }
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 评论朋友圈
     *
     * @param release 评论内容
     */
    private void getCircleRelease(String release) {
        showLoading();
        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.DYNAMIC_COMMENT_USERCODE, ForPreference.getString(ForContents.USER_CODE));
        formMap.put(HttpKey.DYNAMIC_COMMENT_ID, fcid + "");
        formMap.put(HttpKey.DYNAMIC_COMMENT_COMMENTS, release);
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.FRIEND_CIRCLE_COMMENTS, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
                hideLoading();
            }

            @Override
            public void onResponse(String response) {
                ForLog.e(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        getFriendCircleInfo();
                        bool_circle_refresh = true;
                        ForToast.showToast(R.string.aiticle_details_recomment_success);
                        editText_dynamic_details_comment.setText("");
                        isKeyboardShownToHideKeyboard();
                        textView_dynamic_recommend_details.setText(++commentNum + "");
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                    hideLoading();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 获取朋友圈信息
     */
    private void getFriendCircleInfo() {
        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.DYNAMIC_RECOMMENT_FCID, fcid);
        formMap.put(HttpKey.DYNAMIC_RECOMMENT_USERCODE, ForPreference.getString(ForContents.USER_CODE));
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.FRIEND_CIRCLE_INFO, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
                multiStateView_dynamic_details_comment.setViewState(MultiStateView.VIEW_STATE_NOINTERNET);
            }

            @Override
            public void onResponse(String response) {
                ForLog.i(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        friendCircleInfoBean = new Gson().fromJson(response, FriendCircleInfoBean.class);
                        if (friendCircleInfoBean.getFriendcircl() != null) {
                            if (listView_circle_details_data.getHeaderViewsCount() == 1) {
                                listView_circle_details_data.addHeaderView(header_view_2);
                                getDate(friendCircleInfoBean);
                            }
                            circleCommentAdapter.setList(friendCircleInfoBean.getFriendchat());
                            multiStateView_dynamic_details_comment.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        } else {
                            multiStateView_dynamic_details_comment.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                        }
                    } else {
                        multiStateView_dynamic_details_comment.setViewState(MultiStateView.VIEW_STATE_ERROR);
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 回复朋友圈评论
     *
     * @param release 评论内容
     */
    private void getCircleReplys(String release) {
        showLoading();
        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.DYNAMIC_replys_replysid, entity.getReplysid() + "");
        formMap.put(HttpKey.DYNAMIC_replys_id, fcid);
        formMap.put(HttpKey.DYNAMIC_replys_replyscontent, release);
        formMap.put(HttpKey.DYNAMIC_replys_commentsid, entity.getCommentsid() + "");
        formMap.put(HttpKey.DYNAMIC_replys_commentsusercode, entity.getCusercode());
        formMap.put(HttpKey.DYNAMIC_replys_commentsusername, entity.getCname());
        formMap.put(HttpKey.DYNAMIC_replys_replysusercode, ForPreference.getString(ForContents.USER_CODE));
        formMap.put(HttpKey.DYNAMIC_replys_replysusername, ForPreference.getString(ForContents.USER_NAME));
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.FRIEND_CIRCLE_REPLYS, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
                hideLoading();
            }

            @Override
            public void onResponse(String response) {
                ForLog.e(TAG+"getCircleReplys", response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        getFriendCircleInfo();
                        bool_circle_refresh = true;
                        ForToast.showToast(R.string.aiticle_details_recomment_success);
                        editText_dynamic_details_comment.setText("");
                        isKeyboardShownToHideKeyboard();
                        textView_dynamic_recommend_details.setText(++commentNum + "");
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                    hideLoading();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getDate(FriendCircleInfoBean friendCircleInfoBean) {
        String[] split2 = new String[]{};
        if (!friendCircleInfoBean.getFriendcircl().getImgurl().equals("") && !
                friendCircleInfoBean.getFriendcircl().getImgurl().equals("null") &&
                friendCircleInfoBean.getFriendcircl().getImgurl() != null) {
            split2 = friendCircleInfoBean.getFriendcircl().getImgurl().split(",");
        }

        setTitleName(friendCircleInfoBean.getFriendcircl().getName() + " 的动态");

        if (friendCircleInfoBean.getFriendcircl().getContent().equals("") || friendCircleInfoBean.getFriendcircl().getContent() == null){
            textView_article_details_content.setVisibility(View.GONE);
        }else {
            textView_article_details_content.setVisibility(View.VISIBLE);
            textView_article_details_content.setText(friendCircleInfoBean.getFriendcircl().getContent());
        }

        textView_dynamic_creattime_details.setText(mistiming(friendCircleInfoBean.getFriendcircl().getPublisdate()));
        textView_dynamic_zannum_details.setText(friendCircleInfoBean.getFriendcircl().getPraisenumber() + "");
        textView_dynamic_recommend_details.setText(friendCircleInfoBean.getFriendcircl().getViewnumber() + "");
        commentNum = friendCircleInfoBean.getFriendchat().size();

        aBoolean = friendCircleInfoBean.getFriendcircl().getPraisestate() == 1;

        imageView_dynamic_zan_details.setImageResource(friendCircleInfoBean.getFriendcircl().getPraisestate() == 1 ?
                R.drawable.icon_dianzan_h : R.drawable.icon_dianzan_n);

        if (friendCircleInfoBean.getFriendcircl().getPraisenumber() > 0) {
            linearLayout_dynamic_circle_userzan.setVisibility(View.VISIBLE);
            textView_dynamic_circle_userzan_details.setText(friendCircleInfoBean.getPraisename() + "");
            textView_dynamic_circle_userzan_num.setText("等" + friendCircleInfoBean.getFriendcircl().getPraisenumber() + "人觉得很赞");
        } else {
            linearLayout_dynamic_circle_userzan.setVisibility(View.GONE);
        }

        if (split2.length == 0) {
            iv_ngrid_layout_details.setVisibility(View.GONE);
            iv_oneimage_details.setVisibility(View.GONE);
        } else if (split2.length == 1) {
            iv_ngrid_layout_details.setVisibility(View.GONE);
            iv_oneimage_details.setVisibility(View.VISIBLE);

            handlerOneImage(new Image(split2[0], 250, 250));
        } else {
            iv_ngrid_layout_details.setVisibility(View.VISIBLE);
            iv_oneimage_details.setVisibility(View.GONE);

            List<String> lsists = new ArrayList<>();
            for (int i = 0; i < split2.length; i++) {
                lsists.add(split2[i]);
            }

            iv_ngrid_layout_details.setImagesData(lsists);
        }

        final String[] finalSplit = split2;
        iv_ngrid_layout_details.setOnItemClickListener(new NineGridlayout.OnItemClickListener() {
            @Override
            public void onMultipleImgClick(int position) {
                ArrayList<String> stringList = new ArrayList<String>();
                for (int i = 0; i < finalSplit.length; i++) {
                    stringList.add(finalSplit[i]);
                }

                Intent intent = new Intent(CircleDetailsActivity.this, PreviewCircleImgActivity.class);
                intent.putStringArrayListExtra("urlList", stringList);
                intent.putExtra("position", position);
                startActivity(intent);
            }
        });
        iv_oneimage_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<String> stringList = new ArrayList<String>();
                for (int i = 0; i < finalSplit.length; i++) {
                    stringList.add(finalSplit[i]);
                }

                Intent intent = new Intent(CircleDetailsActivity.this, PreviewCircleImgActivity.class);
                intent.putStringArrayListExtra("urlList", stringList);
                intent.putExtra("position", 1);
                startActivity(intent);
            }
        });

    }

    private void handlerOneImage(Image image) {
        int totalWidth;
        int imageWidth;
        int imageHeight;
        ScreenTools screentools = ScreenTools.instance(this);
        totalWidth = screentools.getScreenWidth() - screentools.dip2px(80);
        imageWidth = screentools.dip2px(image.getWidth());
        imageHeight = screentools.dip2px(image.getHeight());
        if (image.getWidth() <= image.getHeight()) {
            if (imageHeight > totalWidth) {
                imageHeight = totalWidth;
                imageWidth = (imageHeight * image.getWidth()) / image.getHeight();
            }
        } else {
            if (imageWidth > totalWidth) {
                imageWidth = totalWidth;
                imageHeight = (imageWidth * image.getHeight()) / image.getWidth();
            }
        }
        ViewGroup.LayoutParams layoutparams = iv_oneimage_details.getLayoutParams();
        layoutparams.height = imageHeight;
        layoutparams.width = imageWidth;
        iv_oneimage_details.setLayoutParams(layoutparams);
        iv_oneimage_details.setClickable(true);
        iv_oneimage_details.setScaleType(ImageView.ScaleType.FIT_XY);
        iv_oneimage_details.setImageUrl(image.getUrl());

    }

    //判断时间差：新闻发布时间与当前系统时间的差值：如果小于60分钟换算成分钟；大于60分钟，换算成小时；大于24小时，换算成天
    public String mistiming(String time) {
        String date1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());

        SimpleDateFormat CurrentTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        Date beginTime = null;
        try {
            beginTime = CurrentTime.parse(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date endTime = null;
        try {
            endTime = CurrentTime.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //进行判断
        Long shijianCha = beginTime.getTime() - endTime.getTime();
        int count = 0;
        if (shijianCha > 24 * 60 * 60 * 1000) {
            count = (int) (shijianCha / (24 * 60 * 60 * 1000));
            return count + "天前";
        } else if (shijianCha > 60 * 60 * 1000) {
            count = (int) (shijianCha / (60 * 60 * 1000));
            return count + "小时前";
        } else if (shijianCha > 60 * 1000) {
            count = (int) (shijianCha / (60 * 1000));
            return count + "分钟前";
        } else {
            return "刚刚";
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.linearLayout_share_wechatFriend:
                dialog_share.hide();
                ShareWeb(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.linearLayout_share_wechatCircle:
                dialog_share.hide();
                ShareWeb(SHARE_MEDIA.WEIXIN_CIRCLE);
                break;
            case R.id.linearLayout_share_tencentCircle:
                dialog_share.hide();
                ShareWeb(SHARE_MEDIA.QZONE);
                break;
        }
    }

    /**
     * 判断软键盘是否弹起如弹起则隐藏
     */
    private void isKeyboardShownToHideKeyboard() {
        if (SystemUtils.isKeyboardShown(editText_dynamic_details_comment.getRootView())) {
            SystemUtils.hideKeyboard(this, editText_dynamic_details_comment.getApplicationWindowToken());
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (bool_circle_refresh){
                setResult(RESULT_OK, new Intent());
            }
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("宝妈动态");
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("宝妈动态");
    }
}
